package com.winit.flow.repository;

import com.winit.flow.model.Flow;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.CrudRepository;

import java.util.List;

public interface FlowRepository extends CrudRepository<Flow, String> {
    Flow findFirstByName(String name);

    @Query(value = "SELECT * FROM  matrix_flow WHERE bussiness_id = ?1 ORDER BY FIND_IN_SET(id, ?2) ASC ", nativeQuery = true)
    List<Flow> findByBussinessId(String businessId,String childSort);

    List<Flow> findByBussinessId(String businessId);
//    @Query(value = "select * from matrix_flow where id in ?1 order by field(id, ?1)", nativeQuery = true)
    List<Flow> findByIdIn(String[]flowIds);

    @Query(value = "select count(re.req_id) from matrix_flow mf, matrix_flow_req mfr, matrix_requirements re where mf.id = mfr.flow_id and mfr.req_id = re.req_id and mf.id = ?1 and if(?2 IS NULL , TRUE , mfr.req_id != ?2) and re.req_status != 'COMPLETED' and re.req_is_del != '0'", nativeQuery = true)
    int computeAssociatedUnlineReq(String flowId, String excludeReqid);


}
